package day03;

import pojo.Account;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

/**
 * @author Xiao
 * @date 2021/8/20
 */
public class ResultSetDemo02 {
    /**
     * 查询account账户表数据，封装为Account对象中，并且存储到ArrayList集合中。
     */
    public static void main(String[] args) throws Exception{
        String url = "jdbc:mysql:///day03?useSSL=false";
        String username = "root";   // 数据库用户名
        String password = "1234";   // 数据库密码
        Connection conn = DriverManager.getConnection
                (url, username, password);  // 获取连接
        Statement stmt = conn.createStatement();
        String sql = "select * from account";
        ResultSet rs = stmt.executeQuery(sql);
        ArrayList<Account> list = new ArrayList<>();
        while (rs.next()) {
            int id = rs.getInt(1);
            String name = rs.getString(2);
            double money = rs.getDouble(3);
            list.add(new Account(id, name, money));
        }
        for (Account account : list) {
            System.out.println(account);
        }
        rs.close();
        stmt.close();
        conn.close();
    }
}
